常見的 Data type 有 :
| Data Type | discrption | note | 
|---|---|---|
| int | Integer 整數,用 32bits 儲存數據 | 通常用這個 | 
| long | 整數,用 64bits 儲存數據 | 大數據用 | 
| double | 帶有小數的數,用 64bits 儲存數據,精準度較高 | 通常用這個 | 
| float | 帶有小數的數,用 32bits 儲存數據 | |
| char | Character 單一字母 用單引號 | 是 基本型別 | 
| String | a sequence of characters 字串 用雙引號 | 是 class | 
| boolean | true 或 false | 存進去變數 | 
| var | variable 的縮寫 | |
| +可以用在任何資料型別 | 不能用在fields | |
| 不能用在沒有初始值的變數 | 
1 Byte = 8 Bits
"bits" 是 "binary digits" 的縮寫是 0 or 1
1 bit ⇒ 0 or 1
2 bit ⇒ 00 or 01 or 10
…
是 2 的 31次方 ~ -2 的 31次方 -1
2的31次方等於 2,147,483,648
二進制的資料可以跟十進制做交換
每個**
char**
都由一個16位元的Unicode字符表示,因此**char**
型別可以表示65536個不同的字符
public class Main {
    public static void main(String[] args) {
        char lastName = 'R';
        char firstName = 'asdfasg'; //error : Too many characters in character literal
        String myLastName = "Rose";
    }
}
public class Main {
    public static void main(String[] args) {
        var x; //error : Cannot infer type: 'var' on variable without initializer
        int x; //declare variable
				 int x, y, z; //允許多值宣告
        System.out.println("Hello world");
        x = 5; //assignment
        System.out.println(x);
    }
}
由 Java 預定義並保留關鍵字命名,開頭都是小寫,不能與其他 Primitive 共享狀態。
除了以上 8 個 primitive ,其他都是 Non-primitive ( String、Array、Arraylist、Class... )
在 Java 當中,變數的有效範圍稱為 scope
A Java fiels 是 a variable inside a class
Fields 的 scope 是整個 Class
每個 field 可以設定 modifier 為 public 或 private
在 method 內 declare 的 variable 只能由大括弧之間的 code 找到
被稱為 local veriable 局部變數、本地變數、區域變數
在 For loop 內部寫的 for (int i = 0…)中的 i 也是一個 local variable
在 Java 中,Static關鍵字用於內存管理
Static 共享給定 Class 所有物件共享相同的 variable、method,可以節省記憶體空間
可以透過 class 本身訪問 static variable 或執行 static method
顧名思義,他不是動態的,所有 Class 所有的 Object 相同,任何 Static variable 和 method 都屬於 Class 本身。
使用 Static variable 、method 時,不需要先從 Class 中 instantiate 任何 object
修飾符 Modifier 提供有關 Class、variable 額外訊息,分成兩類 :
- fields are usually private
- methods are uaually public
public ( access modifier ) | code 隨處可 access
private ( access modifier ) | code 只能在自己的 class access
protected ( access modifier ) | code 可以在同一個 package 和 subclass 訪問
final (Non-Access Modifiers) | attributes 和 methods 不能被覆蓋/修改
常跟 protected final 一起用 
static (Non-Access Modifiers) | attributes 和 methods 屬於 class,而不是從這個 class 做 instantiation 的 object
//常用
public static final int I = 0;
abstract ( Non-Access Modifiers ) | 只能用在 abstract class 中,並且只能用在 methods 上
public class Circle {
		//預設不知道是哪個
    double radius;
    public Circle(double) {
    }
}
有 index 的特性
for any String index 0, 1, 2, 3, … n-1 where n = String.length();
public class Main {
    public static void main(String[] args) {
        String name = "wilson"; //index
        //method
        System.out.println(name.length());//6
        System.out.println(name.toLowerCase());//wilson
        System.out.println(name.toUpperCase());//WILSON
        //String 類的方法 charAt(int index) 返回字串指定 index value
        System.out.println(name.charAt(name.length()- 1));//n
        System.out.println(name.indexOf('s'));//3
        System.out.println(name.indexOf("il"));//1 從哪裡開始找到,而且要用""
        String age = "25";
        System.out.println(Integer.parseInt(age));//型態為 int 的 25
    }
Java 當中的 Relational & Conditional 運算子 operator 會根據 operands 的值 return 一個 boolean (ture、false) 的值
常見的 Relational Operator :
== 用於 primitive data type 的比較
.equals() 用於 Non-primitive data type 的比較
(單個 = 是 assignment 賦值)
!=
是一個 unary operator ( 負負得正 )
3.>、<
4. <=、>=
常見的 Conditional Operator :
public class Barnd02 {
	public static void main(String[] args) {
		System.out.println((5 > 3) && (3 < 4)); 
		//可多個串在一起判斷邏輯
	}
}
public class Main {
    public static void main(String[] args) {
        System.out.println( "wilson" == "wilson" );//ture
        String a = new String ("Wilson");
        String b = new String ("Wilson");
        System.out.println(a == b); //false
        System.out.println("Wilson".equals("Wilson"));//ture
    }
}